package com.carlinfo.demo.dao.impl;

import java.sql.ResultSet;


import java.sql.SQLException;
import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.pojo.AKing;

public class AKingDaoImpl extends BaseDaoImpl<AKing> implements IAKingDao
{

	protected AKing converFromDb(ResultSet rs) throws SQLException
	{
		/* 查询数据 */
		int id = rs.getInt("id");
		int dynastyId = rs.getInt("dynastyId");
		String name = rs.getString("name");
		int age = rs.getInt("age");
		double height = rs.getDouble("height");
		String miaohao = rs.getString("miaohao");
		String nianhao = rs.getString("nianhao");
		String context = rs.getString("context");
		byte status = rs.getByte("status");
		Date createTime = rs.getTimestamp("createTime");
		Date updateTime = rs.getTimestamp("updateTime");
		Date pubTime = rs.getTimestamp("pubTime");
		AKing King = new AKing();
		King.setId(id);
		King.setDynastyId(dynastyId);
		King.setName(name);
		King.setAge(age);
		King.setHeight(height);
		King.setMiaoHao(miaohao);
		King.setNianHao(nianhao);
		King.setContext(context);
		King.setStatus(status);
		King.setCreateTime(createTime);
		King.setUpdateTime(updateTime);
		King.setPubTime(pubTime);
		return King;

	}

	@Override
	public int saveOneDao(AKing King)
	{
		List<Object> paramsList = new ArrayList<Object>();

		String sql = "insert into a_king values(null,?,?,?,?,?,?,?,?,?,?,?)";
		/* 为占位符赋值 */
		paramsList.add(King.getDynastyId());
		paramsList.add(King.getName());
		paramsList.add(King.getAge());
		paramsList.add(King.getHeight());
		paramsList.add(King.getMiaoHao());
		paramsList.add(King.getNianHao());
		paramsList.add(King.getContext());
		paramsList.add(King.getStatus());
		paramsList.add(King.getCreateTime());
		paramsList.add(King.getUpdateTime());
		paramsList.add(King.getPubTime());
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 没有了Id */
		int res = this.executeJDBC(sql, paramsList, resultMap);
		King.setId(Integer.valueOf(resultMap.get("id") + ""));
		return res;
	}

	@Override
	public int updateOneDao(AKing King)
	{
		String sql = "update a_king set dynastyId = ? , name = ? , "
				+ "age = ? , height = ? ,miaoHao = ? , nianHao = ? , " + " context = ? , status = ? , "
				+ "createTime = ? , updateTime = ? , pubTime = ? " + "where id = ? ";
		List<Object> paramsList = new ArrayList<Object>();
		/* 为占位符赋值 */
		paramsList.add(King.getDynastyId());
		paramsList.add(King.getName());
		paramsList.add(King.getAge());
		paramsList.add(King.getHeight());
		paramsList.add(King.getMiaoHao());
		paramsList.add(King.getNianHao());
		paramsList.add(King.getContext());
		paramsList.add(King.getStatus());
		paramsList.add(King.getCreateTime());
		paramsList.add(King.getUpdateTime());
		paramsList.add(King.getPubTime());
		paramsList.add(King.getId());

		return this.executeJDBC(sql, paramsList);
	}

	/**
	 * 根据id删除 delete from a_king where id = ? delect from a_king where name = ?
	 */
	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "delete from a_king ";
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql = sql + " where id = ? ";
			paramsList.add(condMap.get("id"));
		} else if (condMap.get("name") != null && !"".equalsIgnoreCase(condMap.get("name") + ""))
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}

		return this.executeJDBC(sql, paramsList);
	}

	@Override
	public AKing selectOneDao(Map<String, Object> condMap)
	{
		/* 准备一个容器,这里面放的就是参数 */
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_king ";
		if (!"".equalsIgnoreCase(condMap.get("id") + "") && condMap.get("id") != null)
		{
			sql = sql + " where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (!"".equalsIgnoreCase(condMap.get("name") + "") && condMap.get("name") != null)
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}

		/* 调用 converFromDb先初始化pojo */
		this.pojoObj = new AKing();
		return this.findOneJDBC(sql, paramsList);
	}

	/**
	 * 分页: ~想办法把SQL语句加上limit
	 */
	public List<AKing> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		List<AKing> KingList = new ArrayList<AKing>();
		/* sql语句 */
		String sql = "select * from a_king where 1 = 1";
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and (name like ? or context like ?)";
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql += " and status = ? ";
			paramsList.add(condMap.get("status"));
		}
		/*
		 * 时间志勇判断是不是Date类型就可以 null intanceof Date:返回值是true还是false
		 */
		if (condMap.get("stDate") instanceof Date && condMap.get("esDate") instanceof Date)
		{
			sql += " and ( pubTime >= ? and pubTime < ? )";
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("edDate"));
		}
		/* 排序 */
		if ("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		} else if ("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:降序 */
			sql += " order by id asc, name desc";
		} else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		/* 调用 converFromDb先初始化pojo*/
		this.pojoObj = new AKing() ; 
		/*
		 * 分页:传PageInfo 不分页:不传PageInfo
		 */
		if (pageInfoUtil != null)
		{
			/* 分页 */
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		/* 不分页 */
		return this.findList(sql, paramsList);
	}

}
